#ifndef __DILITHIUM_IMPL_H
#define __DILITHIUM_IMPL_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

/* Dilithium2Parameters（NIST标准） */
#define DILITHIUM_N           256
#define DILITHIUM_Q           8380417
#define DILITHIUM_D           13
#define DILITHIUM_ROOT_OF_UNITY 1753
#define DILITHIUM_SEEDBYTES   32
#define DILITHIUM_CRHBYTES    64
#define DILITHIUM_K           4
#define DILITHIUM_L           4
#define DILITHIUM_ETA         2
#define DILITHIUM_TAU         39
#define DILITHIUM_BETA        78
#define DILITHIUM_GAMMA1      (1 << 17)
#define DILITHIUM_GAMMA2      ((DILITHIUM_Q-1)/88)
#define DILITHIUM_OMEGA       80

#define DILITHIUM_POLYT1_PACKEDBYTES  320
#define DILITHIUM_POLYT0_PACKEDBYTES  416
#define DILITHIUM_POLYVECH_PACKEDBYTES (DILITHIUM_OMEGA + DILITHIUM_K)

#define DILITHIUM_POLYZ_PACKEDBYTES   576

#define DILITHIUM_POLYW1_PACKEDBYTES  192

#define DILITHIUM_POLYETA_PACKEDBYTES  96

#define DILITHIUM_KEY_SIZE    32
#define DILITHIUM_SIG_SIZE    64

/* 多项式结构 */
typedef struct {
    int32_t coeffs[DILITHIUM_N];
} Poly;

/* 多项式Vector */
typedef struct {
    Poly vec[DILITHIUM_K];
} PolyVecK;

typedef struct {
    Poly vec[DILITHIUM_L];
} PolyVecL;

/* Dilithium上下文 */
typedef struct {
    uint8_t rho[DILITHIUM_SEEDBYTES];
    uint8_t key[DILITHIUM_SEEDBYTES];
    uint8_t tr[DILITHIUM_CRHBYTES];
    PolyVecL s1;
    PolyVecK s2;
    PolyVecK t0;
    uint8_t key_loaded;
} Dilithium_Context;

/* 签名大小（标准Format） */
#define DILITHIUM_CRYPTO_BYTES (DILITHIUM_SEEDBYTES + DILITHIUM_L*DILITHIUM_POLYZ_PACKEDBYTES + DILITHIUM_POLYVECH_PACKEDBYTES)

/* Function声明 */

/**
 * @brief InitializeDilithium上下文
 */
void Dilithium_Impl_Init(Dilithium_Context* ctx);

/**
 * @brief LoadDilithiumKey
 */
void Dilithium_LoadKey(Dilithium_Context* ctx, const uint8_t* seed);

/**
 * @brief Dilithium签名（标准实现）
 */
void Dilithium_Sign(const Dilithium_Context* ctx, const uint8_t* msg, uint8_t* signature);

/**
 * @brief DilithiumVerify（标准实现）
 */
int Dilithium_Verify(const Dilithium_Context* ctx, const uint8_t* msg, const uint8_t* signature);

#ifdef __cplusplus
}
#endif

#endif /* __DILITHIUM_IMPL_H */
